package com.cssy.xxkj.basic.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.byd.acegiext.domain.User;
import com.cssy.xxkj.basic.dao.BasicStorageInfoTestDAO;
import com.cssy.xxkj.basic.dao.BasicStorageLocationDAO;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTypeTestDO;
import com.cssy.xxkj.basic.entity.BasicStorageLocationDO;
import com.cssy.xxkj.basic.entity.BasicWorkshopDO;
import com.cssy.xxkj.basic.service.IBasicStorageLocationService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.IDCodeTypeUtil;


@Service("basicStorageLocationService")
public class BasicStorageLocationServiceImpl implements IBasicStorageLocationService{

	
	@Resource(name="basicStorageLocationDAO")	
	private BasicStorageLocationDAO basicStorageLocationDAO;
    @Resource(name="basicStorageInfoTestDAO")
    private BasicStorageInfoTestDAO basicStorageInfoTestDAO;
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<BasicStorageLocationDO>  list=basicStorageLocationDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			Map<String,BasicStorageInfoTestDO> map=this.rtWorkshop();
			for(int i=0;i<list.size();i++){
				if(list.get(i).getZd003WhId()!=null&&!"".equals(list.get(i).getZd003WhId())){
					if(map!=null){
						list.get(i).setBasicStorageInfoTestDO(map.get(list.get(i).getZd003WhId()));
					}
				}
			}
		}
		rs.setRecordCount(basicStorageLocationDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return basicStorageLocationDAO.deleteBasicStoragelocationDOByPrimaryKey(id);
	}

	@Override
	public Integer update(BasicStorageLocationDO t, User user)
			throws SyBaseException {
		t.setBizModifyName(user.getName());
		t.setBizModifyDate(new Date());
		t.setBizVersionMark(t.getBizVersion());
		t.setBizVersion(t.getBizVersion()+1);
		return basicStorageLocationDAO.updateBasicStoragelocationDO(t);
	}

	@Override
	public BasicStorageLocationDO save(BasicStorageLocationDO t, User user)
			throws SyBaseException {
		String id=IDCodeTypeUtil.getId();
		t.setId(id);
		t.setBizCreatorName(user.getName());
		t.setBizCreatorDate(new Date());
		t.setBizVersion(1);
		basicStorageLocationDAO.insertBasicStoragelocationDO(t);
		return t;
	}

	@Override
	public BasicStorageLocationDO findListById(String id)
			throws SyBaseException {
		return basicStorageLocationDAO.findBasicStoragelocationDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return basicStorageLocationDAO.deleteBizRemoved(id);
	}
	private Map<String, BasicStorageInfoTestDO> rtWorkshop(){
		Map<String,BasicStorageInfoTestDO> map = new HashMap<String, BasicStorageInfoTestDO>();
		 SearchConditions scs = new SearchConditions();
		 scs.setRemovedProperty(null);
		List<BasicStorageInfoTestDO> list = basicStorageInfoTestDAO.find(scs);
		if(list!=null&&!list.isEmpty()&&list.size()>0){
			for(int i=0;i<list.size();i++){
				map.put(list.get(i).getId(), list.get(i));
			}
		}else{
			return null;
		}
		return map;
	}
	
	
}
